﻿using System.Diagnostics;
using Newtonsoft.Json;
using System;

namespace OperationPlugins.WebApp.ViewModes
{
    public abstract class ViewModelBase
    {

        protected ViewModelBase()
        {
            Messages = new MessageContainer();    
        }

        /// <summary>
        /// Contains all information and error messages.
        /// </summary>
        public MessageContainer Messages { get; set; }

        /// <summary>
        /// Redirect after post?
        /// </summary>
        public bool Redirect { get; set; }

        /// <summary>
        /// Redirect url.
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// Is the view-model dirty?
        /// </summary>
        public bool IsDirty { get; set; }

        /// <summary>
        /// IViewModelResourceProvider.
        /// </summary>
        [JsonIgnore]
        internal IViewModelResourceProvider ResourceProvider { get; set; }

        /// <summary>
        /// Redirect to the specified url.
        /// </summary>
        /// <param name="url">Url.</param>
        internal void RedirectTo(string url)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            Redirect = true;
            Url = url;
        }

        internal protected void DoAction(Action action, string primaryErrorMessage = null)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            try
            {
                action.Invoke();
            }
            catch (Exception ex)
            {
                // TODO: Log dxception.
                Debugger.Break();
            }
        }



    }
}